package com.example.mycalculator;

import androidx.appcompat.app.AppCompatActivity;

//import android.annotation.SuppressLint;
//import android.content.Intent;
import android.util.Log;
import android.view.View;
import  android.widget.*;
import android.os.Bundle;
import java.util.*;

import javax.security.auth.login.LoginException;

//import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener {

    private TextView textView;

    private ArrayList data = new ArrayList();

    private ArrayList symbol = new ArrayList();


    private double dbe_previously = 0;

    boolean yn=false;



    double process=0;

    int cal_f_long=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_0 = findViewById(R.id.btn_0);
        btn_0.setOnClickListener(this);
        Button btn_1 = findViewById(R.id.btn_1);
        btn_1.setOnClickListener(this);
        Button btn_2 = findViewById(R.id.btn_2);
        btn_2.setOnClickListener(this);
        Button btn_3 = findViewById(R.id.btn_3);
        btn_3.setOnClickListener(this);
        Button btn_4 = findViewById(R.id.btn_4);
        btn_4.setOnClickListener(this);
        Button btn_5 = findViewById(R.id.btn_5);
        btn_5.setOnClickListener(this);
        Button btn_6 = findViewById(R.id.btn_6);
        btn_6.setOnClickListener(this);
        Button btn_7 = findViewById(R.id.btn_7);
        btn_7.setOnClickListener(this);
        Button btn_8 = findViewById(R.id.btn_8);
        btn_8.setOnClickListener(this);
        Button btn_9 = findViewById(R.id.btn_9);
        btn_9.setOnClickListener(this);
        Button btn_dp = findViewById(R.id.btn_dp);
        btn_dp.setOnClickListener(this);
        //加减乘除
        Button btn_add = findViewById(R.id.btn_add);
        btn_add.setOnClickListener(this);
        Button btn_subtract = findViewById(R.id.btn_subtract);
        btn_subtract.setOnClickListener(this);
        Button btn_multiply = findViewById(R.id.btn_multiply);
        btn_multiply.setOnClickListener(this);
        Button btn_except = findViewById(R.id.btn_except);
        btn_except.setOnClickListener(this);
        //功能键
        Button btn_pan = findViewById(R.id.btn_pan);
        btn_pan.setOnClickListener(this);
        Button btn_clear = findViewById(R.id.btn_clear);
        btn_clear.setOnClickListener(this);
        Button btn_c = findViewById(R.id.btn_c);
        btn_c.setOnClickListener(this);
        Button btn_ce = findViewById(R.id.btn_ce);
        btn_ce.setOnClickListener(this);
        Button btn_bf = findViewById(R.id.btn_bf);
        btn_bf.setOnClickListener(this);
        Button btn_fm = findViewById(R.id.btn_fm);
        btn_fm.setOnClickListener(this);
        Button btn_amount=findViewById(R.id.btn_amount);
        btn_amount.setOnClickListener(this);
        //在onClick中更改cal_display中的text要加这个，原理未知
        textView = (TextView) findViewById(R.id.cal_display);
        TextView cal_display = findViewById(R.id.cal_display);
        TextView cal_formula = findViewById(R.id.cal_formula);
        cal_display.setText("0");
        cal_formula.setText("");
    }

    @Override
    public void onClick(View view) {
        textView = (TextView) findViewById(R.id.cal_display);
        TextView cal_display = findViewById(R.id.cal_display);
        TextView cal_formula = findViewById(R.id.cal_formula);
        if(yn==false){
            cal_display.setText("");
        }
        switch (view.getId()) {
            case R.id.btn_1:
                cal_display.append("1");
                yn = true;
                break;
            case R.id.btn_2:
                cal_display.append("2");
                yn = true;
                break;
            case R.id.btn_3:
                cal_display.append("3");
                yn = true;
                break;
            case R.id.btn_4:
                cal_display.append("4");
                yn = true;
                break;
            case R.id.btn_5:
                cal_display.append("5");
                yn = true;
                break;
            case R.id.btn_6:
                cal_display.append("6");
                yn = true;
                break;
            case R.id.btn_7:
                cal_display.append("7");
                yn = true;
                break;
            case R.id.btn_8:
                cal_display.append("8");
                yn = true;
                break;
            case R.id.btn_9:
                cal_display.append("9");
                yn = true;
                break;
            case R.id.btn_0:
                Log.i("卡", cal_display.getText().toString());
                if (!cal_display.getText().toString().equals("0")) {
                    cal_display.append("0");
                }
                yn = true;
                break;
            case R.id.btn_dp:
                if (!cal_display.getText().toString().contains(".")) {
                    if (yn) {
                        cal_display.append(".");
                    } else {
                        cal_display.append("0.");
                    }
                }
                yn = true;
                break;//
        }//修正%号效果
        if(dbe_previously%1==0&data.size()!=0){
            cal_f_long=String.valueOf(dbe_previously).length()-1;
        }else{
            cal_f_long=String.valueOf(dbe_previously).length()+1;
        }
        if(yn==true&&cal_formula.getText().toString().length()>cal_f_long){
            cal_formula.setText(cal_formula.getText().toString().substring(0,cal_f_long));
        }
        switch (view.getId()){// 运算符============================================================================================
            case R.id.btn_add://加
                if(cal_display.getText().toString()!=""){
                    if(yn==true){
                        data.add(Double.parseDouble(cal_display.getText().toString()));
                        symbol.add("+");
                        if(symbol.size()>1){
                            if(symbol.get(symbol.size()-2).toString().equals("-")){
                                dbe_previously=dbe_previously-(double)data.get(data.size()-1);
                            }else if(symbol.get(symbol.size()-2).toString().equals("*")){
                                dbe_previously=dbe_previously*(double)data.get(data.size()-1);
                            }else if(symbol.get(symbol.size()-2).toString().equals("/")){
                                if((double)data.get(data.size()-1)!=0){
                                    dbe_previously=dbe_previously/(double)data.get(data.size()-1);
                                }else{
                                    cal_formula.append("0+");
                                    cal_display.setText("除数不能为0");
                                    dbe_previously=0;
                                    symbol.clear();
                                    data.clear();
                                    yn=false;
                                    break;
                                }
                            }else{
                                dbe_previously=dbe_previously+(double)data.get(data.size()-1);
                            }
                        }else{
                            dbe_previously=dbe_previously+(double)data.get(data.size()-1);
                        }
                        if(dbe_previously%1!=(double) 0.0){
                            cal_formula.setText(String.valueOf(dbe_previously)+"+");
                            cal_display.setText(String.valueOf(dbe_previously));
                        }else{
                            cal_formula.setText(String.valueOf((int) dbe_previously)+"+");
                            cal_display.setText(String.valueOf((int) dbe_previously));
                        }
                        yn=false;
                    }else{
                        symbol.add(symbol.size()-1,"+");
                        cal_formula.setText(String.valueOf((int)dbe_previously)+"+");
                    }
                }else{
                    cal_display.setText("0");
                    cal_formula.setText(String.valueOf((int)dbe_previously)+"+");
                }
                break;
            case R.id.btn_subtract:
                if(cal_display.getText().toString()!=""){
                    if(yn==true){
                        data.add(Double.parseDouble(cal_display.getText().toString()));
                        symbol.add("-");
                        if(symbol.size()>1){
                            if(symbol.get(symbol.size()-2).toString().equals("-")){
                                dbe_previously=dbe_previously-(double)data.get(data.size()-1);
                            }else if(symbol.get(symbol.size()-2).toString().equals("*")){
                                dbe_previously=dbe_previously*(double)data.get(data.size()-1);
                            }else if(symbol.get(symbol.size()-2).toString().equals("/")){
                                if((double)data.get(data.size()-1)!=0){
                                    dbe_previously=dbe_previously/(double)data.get(data.size()-1);
                                }else{
                                    cal_formula.append("0-");
                                    cal_display.setText("除数不能为0");
                                    dbe_previously=0;
                                    symbol.clear();
                                    data.clear();
                                    yn=false;
                                    break;
                                }
                            }else{
                                dbe_previously=dbe_previously+(double)data.get(data.size()-1);
                            }
                        }else{
                            dbe_previously=dbe_previously+(double)data.get(data.size()-1);
                        }
                        if(dbe_previously%1!=(double) 0.0){
                            cal_formula.setText(String.valueOf(dbe_previously)+"-");
                            cal_display.setText(String.valueOf(dbe_previously));
                        }else{
                            cal_formula.setText(String.valueOf((int) dbe_previously)+"-");
                            cal_display.setText(String.valueOf((int) dbe_previously));
                        }
                        yn=false;
                    }else{
                        symbol.add(symbol.size()-1,"+");
                        cal_formula.setText(String.valueOf((int)dbe_previously)+"-");
                        //cal_formula.setText(cal_formula.getText().toString().substring(0,cal_formula.getText().toString().length()-1)+"-");
                    }
                }else{
                    cal_display.setText("0");
                    cal_formula.setText(String.valueOf((int)dbe_previously)+"-");
                }
                break;
            case R.id.btn_multiply://乘
                if(cal_display.getText().toString()!=""){
                    if(yn==true){
                        data.add(Double.parseDouble(cal_display.getText().toString()));
                        symbol.add("*");
                        if(symbol.size()>1){
                            if(symbol.get(symbol.size()-2).toString().equals("-")){
                                dbe_previously=dbe_previously-(double)data.get(data.size()-1);
                            }else if(symbol.get(symbol.size()-2).toString().equals("*")){
                                dbe_previously=dbe_previously*(double)data.get(data.size()-1);
                            }else if(symbol.get(symbol.size()-2).toString().equals("/")){
                                if((double)data.get(data.size()-1)!=0){
                                    dbe_previously=dbe_previously/(double)data.get(data.size()-1);
                                }else{
                                    cal_formula.append("0*");
                                    cal_display.setText("除数不能为0");
                                    dbe_previously=0;
                                    symbol.clear();
                                    data.clear();
                                    yn=false;
                                    break;
                                }
                            }else{
                                dbe_previously=dbe_previously+(double)data.get(data.size()-1);
                            }
                        }else{
                            dbe_previously=dbe_previously+(double)data.get(data.size()-1);
                        }
                        if(dbe_previously%1!=(double) 0.0){
                            cal_formula.setText(String.valueOf(dbe_previously)+"*");
                            cal_display.setText(String.valueOf(dbe_previously));
                        }else{
                            cal_formula.setText(String.valueOf((int) dbe_previously)+"*");
                            cal_display.setText(String.valueOf((int) dbe_previously));
                        }
                        yn=false;
                    }else{
                        symbol.add(symbol.size()-1,"*");
                        cal_formula.setText(String.valueOf((int)dbe_previously)+"*");
                    }
                }else{
                    cal_display.setText("0");
                    cal_formula.setText(String.valueOf((int)dbe_previously)+"*");
                }
                break;
            case R.id.btn_except://除
                if(cal_display.getText().toString()!=""){
                    if(yn==true){
                        data.add(Double.parseDouble(cal_display.getText().toString()));
                        symbol.add("/");
                        if(symbol.size()>1){
                            if(symbol.get(symbol.size()-2).toString().equals("-")){
                                dbe_previously=dbe_previously-(double)data.get(data.size()-1);
                            }else if(symbol.get(symbol.size()-2).toString().equals("*")){
                                dbe_previously=dbe_previously*(double)data.get(data.size()-1);
                            }else if(symbol.get(symbol.size()-2).toString().equals("/")){
                                if((double)data.get(data.size()-1)!=0){
                                    dbe_previously=dbe_previously/(double)data.get(data.size()-1);
                                }else{
                                    cal_formula.append("0/");
                                    cal_display.setText("除数不能为0");
                                    dbe_previously=0;
                                    symbol.clear();
                                    data.clear();
                                    yn=false;
                                    break;
                                }
                            }else{
                                dbe_previously=dbe_previously+(double)data.get(data.size()-1);
                            }
                        }else{
                            dbe_previously=dbe_previously+(double)data.get(data.size()-1);
                        }
                        if(dbe_previously%1!=(double) 0.0){
                            cal_formula.setText(String.valueOf(dbe_previously)+"/");
                            cal_display.setText(String.valueOf(dbe_previously));
                        }else{
                            cal_formula.setText(String.valueOf((int) dbe_previously)+"/");
                            cal_display.setText(String.valueOf((int) dbe_previously));
                        }
                        yn=false;
                    }else{
                        symbol.add(symbol.size()-1,"/");
                        cal_formula.setText(String.valueOf((int)dbe_previously)+"/");
                    }
                }else{
                    cal_display.setText("0");
                    cal_formula.setText(String.valueOf((int)dbe_previously)+"/");
                }
                break;
            case R.id.btn_pan://改变正负号\
                if(cal_display.getText().toString()!="0"){
                    if(cal_display.getText().toString().substring(0,1).equals("-")){
                        cal_display.setText(cal_display.getText().toString().substring(1));
                    }else{
                        cal_display.setText("-"+cal_display.getText().toString());
                    }
                }
                break;
            case R.id.btn_clear://叉
                if(!cal_display.getText().toString().equals("0")&yn==true){
                    if(cal_display.getText().toString().length()==1){
                        cal_display.setText("0");
                        yn=false;
                    }else{
                        cal_display.setText(cal_display.getText().toString().substring(0,cal_display.getText().toString().length()-1));
                    }
                }
                break;
            case R.id.btn_c:
                yn=false;
                data.clear();
                symbol.clear();
                dbe_previously=0;
                cal_formula.setText("");
                cal_display.setText("0");
                break;
            case R.id.btn_ce:
                yn=false;
                cal_display.setText("0");
                break;
            case R.id.btn_bf://%
                if(cal_display.getText().toString().equals("")){
                    cal_display.setText("0");
                }
                process=Double.parseDouble(cal_display.getText().toString())/100*dbe_previously;
                if(process%1==0){
                    if(!cal_formula.getText().toString().equals("0")){
                        cal_formula.setText(cal_formula.getText().toString()+(int)process);
                    }
                    cal_display.setText(String.valueOf((int)process));
                    yn=false;
                }else{
                    cal_formula.setText(cal_formula.getText().toString()+process);
                    cal_display.setText(String.valueOf(process));
                    yn=false;
                }
                break;
            case R.id.btn_fm:
                if(cal_display.getText().toString().equals("")||Double.parseDouble(cal_display.getText().toString())==0) {
                    cal_display.setText("除数不能为0");
                }else{
                    cal_formula.setText("1/("+cal_display.getText().toString()+")");
                    cal_display.setText(String.valueOf(1/Double.valueOf(cal_display.getText().toString())));
                }
                break;
            case R.id.btn_amount:
                if(cal_display.getText().toString()!=""){
                    if(yn==true){
                        data.add(Double.parseDouble(cal_display.getText().toString()));
                        if(symbol.size()>1){
                            if(symbol.get(symbol.size()-2).toString().equals("-")){
                                dbe_previously=dbe_previously-(double)data.get(data.size()-1);
                            }else if(symbol.get(symbol.size()-2).toString().equals("*")){
                                dbe_previously=dbe_previously*(double)data.get(data.size()-1);
                            }else if(symbol.get(symbol.size()-2).toString().equals("/")){
                                if((double)data.get(data.size()-1)!=0){
                                    dbe_previously=dbe_previously/(double)data.get(data.size()-1);
                                }else{
                                    cal_formula.append("0");
                                    cal_display.setText("除数不能为0");
                                    dbe_previously=0;
                                    symbol.clear();
                                    data.clear();
                                    yn=false;
                                    break;
                                }
                            }else{
                                dbe_previously=dbe_previously+(double)data.get(data.size()-1);
                            }
                        }else{
                            dbe_previously=dbe_previously+(double)data.get(data.size()-1);
                        }
                        if(dbe_previously%1!=(double) 0.0){
                            cal_formula.setText(cal_formula.getText().toString()+cal_display.getText().toString()+"="+String.valueOf(dbe_previously));
                            cal_display.setText(String.valueOf(dbe_previously));
                        }else{
                            cal_formula.setText(cal_formula.getText().toString()+cal_display.getText().toString()+"="+String.valueOf((int)dbe_previously));
                            cal_display.setText(String.valueOf((int) dbe_previously));
                        }
                        yn=false;
                    }
                }else{
                    cal_display.setText("0");
                    cal_formula.setText("0=");
                }
                break;
        }
    }
}